<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('api服务列表')" />
	<th:block th:include="include :: layout-latest-css" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">

	<div class="ui-layout-west">
		<div class="box box-main">
			<div class="box-header">
				<div class="box-title">
					<i class="fa icon-grid"></i> 服务组
				</div>
				<div class="box-tools pull-right">
					<button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新"><i class="fa fa-refresh"></i></button>
				</div>
			</div>
			<div class="ui-layout-content">
				<div id="tree" class="ztree"></div>
			</div>
		</div>
	</div>

	<div class="ui-layout-center">
		<div class="container-div">
			<div class="row">
				<div class="col-sm-12 search-collapse">
					<form id="formId">
						<input type="hidden" name="apiGroupId" id="apiGroupId"/>
						<div class="select-list">
							<ul>
								<li>
									api名称：<input type="text" name="apiName"/>
								</li>
	
								<li>
									api地址：<input type="text" name="apiUrlSuffix"/>
								</li>
	
								<li>
									<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
									<a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
				<div class="col-sm-12 select-table table-striped">
					<table id="bootstrap-table" data-mobile-responsive="true"></table>
				</div>
			</div>
		</div>
	</div>
	
    <div th:include="include :: footer"></div>
    <th:block th:include="include :: layout-latest-js" />
	<th:block th:include="include :: ztree-js" />
	
    <script th:inline="javascript">
        $(function() {
		    var panehHidden = false;
		    if ($(this).width() < 769) {
		        panehHidden = true;
		    }
		    $('body').layout({ initClosed: panehHidden, west__size: 185 });
		    queryServerList();
		    queryGroupTree();
		});
        
        $('#btnRefresh').click(function() {
        	queryGroupTree();
        	$("#apiGroupId").val("");
			$.table.search();
		});
        
        function queryGroupTree()
		{
			var url = ctx + "api/apiGroup/treeData";
			var options = {
		        url: url,
		        expandLevel: 2,
		        callback: {
			        onClick: zOnClick,                      // 用于捕获节点被点击的事件回调函数
		        }
		    };
			$.tree.init2(options);
			
			function zOnClick(event, treeId, treeNode) {
				$("#apiGroupId").val(treeNode.id);
				$.table.search();
			}
		}
        
        function queryServerList() {
            var options = {
                url: ctx + "api/accountApiLimit/choseApiList?accountId="+[[${accountId}]],
                modalName: "api服务",
		        showExport: true,
                columns: [
				{
					field : 'apiId', 
					title : '主键',
					visible: false
				},
				{
					field : 'apiName', 
					title : '服务名称',
					sortable: true
				},
				{
					field : 'apiUrlSuffix', 
					title : 'api地址',
					sortable: true
				},
				{
					field : 'requestUrl', 
					title : '真实请求地址',
					sortable: true
				},
				{
					field : 'apiRequestMethod', 
					title : '请求方式',
					sortable: true
				},
				{
					field : 'remark', 
					title : '备注',
					formatter: function(value, row, index) {
						if(value!=null && value.length>15){
							value = "<span title='"+value+"'>"+value.substring(0,15)+"。。。</span>";
						}
						return value;
		            }
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="choseApi('+row.apiId+')"><i class="fa fa-edit"></i>添加至账户</a> ');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        }
        
        function reset() {
        	var currentId = "formId";
        	$("#" + currentId)[0].reset();
        	$("#apiGroupId").val("");
            $.btTable.bootstrapTable('refresh');
        }
        
        function choseApi(apiId){
        	window.parent.layer.closeAll();
//         	window.parent.layer.closeAll();
//         	window.location.href=ctx + "api/accountApiLimit/add?accountId="+[[${accountId}]]+"&apiId="+apiId;
        	window.parent.$.modal.open("添加api接口权限", ctx + "api/accountApiLimit/add?accountId="+[[${accountId}]]+"&apiId="+apiId);
        }
    </script>
</body>
</html>